import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author 宋贤坤
 * @date 2019/7/4 12:57
 */
public class MouseClickWeatherEnter implements MouseListener {

    private static final String SPACE = "\n";

    public void mouseClicked(MouseEvent e) {

        Main.outPutContentArea.setText(null);

        StringBuilder stringBuilder = new StringBuilder();
        String s;
        try {
            s = HttpUtil.get("http://t.weather.sojson.com/api/weather/city/101270101");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "…(⊙_⊙;)…  掉网了....", ex.getMessage(), JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (StrUtil.isBlank(s)) {
            JOptionPane.showMessageDialog(null, "未获取到当前城市天气,请稍后再试！");
            return;
        }
        JSONObject jsonObject = JSONUtil.parseObj(s);
        Object data = jsonObject.get("data");
        JSONObject dataContent = JSONUtil.parseObj(data);
        Object forecast = dataContent.get("forecast");
        JSONArray objects = JSONUtil.parseArray(forecast);
        stringBuilder.append("今天：");
        stringBuilder.append(SPACE);
        stringBuilder.append(objects.get(0));
        stringBuilder.append(SPACE);
        stringBuilder.append(SPACE);
        stringBuilder.append("明天：");
        stringBuilder.append(SPACE);
        stringBuilder.append(objects.get(1));
        stringBuilder.append(SPACE);
        stringBuilder.append(SPACE);
        stringBuilder.append("后天：");
        stringBuilder.append(SPACE);
        stringBuilder.append(objects.get(2));
        Main.outPutContentArea.setText(stringBuilder.toString());
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}
